﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class PageZoneModuleProcessBll
    {
        public static PageZoneModuleCls[] ReadingModulesInPage(string SiteId, string PageId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null,
                    " select TablePageZoneModule.* from TablePageZoneModule " +
                    "   where frkPageId=@PageId and AssetCode=@AssetCode " +
                    "   order by SortIndex",
                    new DbParam[]{
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("PageId",PageId)
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new PageZoneModuleCls[0];
            }
            PageZoneModuleCls[] CmsPageZoneModules = PageZoneModuleParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsPageZoneModules;
        }

        public static PageZoneModuleCls[] ReadingPageZoneModule(string SiteId, string PageZoneId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePageZoneModule where frkPageZoneId=@PageZoneId and AssetCode=@AssetCode order by SortIndex",
                    new DbParam[]{
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("PageZoneId",PageZoneId)
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new PageZoneModuleCls[0];
            }
            PageZoneModuleCls[] CmsPageZoneModules = PageZoneModuleParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsPageZoneModules;
        }

        public static PageZoneModuleCls CreatePageZoneModuleModel(string SiteId, string PageZoneModuleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePageZoneModule where PageZoneModuleId=@PageZoneModuleId  ",
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneModuleId", PageZoneModuleId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            PageZoneModuleCls OPageZoneModule = PageZoneModuleParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OPageZoneModule;
        }



        public static void AddPageZoneModule(
            string SiteId,
            PageZoneModuleCls OPageZoneModule)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            if (string.IsNullOrEmpty(OPageZoneModule.PageZoneModuleId))
            {
                OPageZoneModule.PageZoneModuleId = System.Guid.NewGuid().ToString();
            }
            DBService.Insert(null, "TablePageZoneModule",
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneModuleId", OPageZoneModule.PageZoneModuleId),
                        new DbParam("frkPageZoneId",OPageZoneModule.PageZoneId),
                        new DbParam("ModuleTitle",OPageZoneModule.ModuleTitle),
                        new DbParam("Visible",OPageZoneModule.Visible),
                        new DbParam("SortIndex",OPageZoneModule.SortIndex),
                        new DbParam("ModuleId",OPageZoneModule.ModuleId),
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("DataLanguage",DataLanguage),
                        new DbParam("frkPageId",OPageZoneModule.PageId),
                        new DbParam("ServiceId",OPageZoneModule.ServiceId),
                        new DbParam("CssClass",OPageZoneModule.CssClass),
                        new DbParam("XmlDataConfig",""),
                    });
        }


        public static void DeletePageZoneModule(string SiteId, string PageZoneModuleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TablePageZoneModule where PageZoneModuleId=@PageZoneModuleId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneModuleId", PageZoneModuleId),
                    });
        }


        public static void SavePageZoneModule(string SiteId, string PageZoneModuleId, PageZoneModuleCls OPageZoneModule)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePageZoneModule", "PageZoneModuleId", PageZoneModuleId,
                    new DbParam[] 
                    { 
                        new DbParam("ModuleTitle",OPageZoneModule.ModuleTitle),
                        new DbParam("Visible",OPageZoneModule.Visible),
                        new DbParam("SortIndex",OPageZoneModule.SortIndex),
                    });
        }

        public static void SavePageZoneModuleView(string SiteId, string PageZoneModuleId, string PlugInViewId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePageZoneModule", "PageZoneModuleId", PageZoneModuleId,
                    new DbParam[] 
                    { 
                        new DbParam("PlugInViewId",PlugInViewId),
                    });
        }

        public static void SavePageZoneForMove(string SiteId, string PageZoneModuleId, string PageZoneId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePageZoneModule", "PageZoneModuleId", PageZoneModuleId,
                    new DbParam[] 
                    { 
                        new DbParam("frkPageZoneId",PageZoneId),
                    });
        }

        public static void SavePageZoneModuleInfo(
            string SiteId,
            PageZoneModuleCls[] PageZoneModules)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            for (int iIndex = 0; iIndex < PageZoneModules.Length; iIndex++)
            {
                DBService.Update(null, "TablePageZoneModule", "PageZoneModuleId", PageZoneModules[iIndex].PageZoneModuleId,
                        new DbParam[] 
                    { 
                        new DbParam("SortIndex",PageZoneModules[iIndex].SortIndex),
                        new DbParam("CssClass",PageZoneModules[iIndex].CssClass),
                    });
            }
        }



        public static void SavePageZoneModuleConfig(
            string SiteId,
            string PageZoneModuleId,
            string XmlDataConfig)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePageZoneModule", "PageZoneModuleId", PageZoneModuleId,
                    new DbParam[] 
                    { 
                        new DbParam("XmlDataConfig",XmlDataConfig),
                    });
        }
    }
}

